/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import models.Course;
import models.CourseFile;
import models.User;
import org.hibernate.Session;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import service.CourseDao;
import service.HibernateUtil;
import service.PaymentDao;
import service.UserDao;

/**
 *
 * @author Robert
 */
public class CourseEnvironmentController extends HttpServlet {

    private HibernateUtil HU = new HibernateUtil();
    private Session session = HU.openSession();
    private CourseDao dao = new CourseDao(session);
    private UserDao udao = new UserDao(session);
    private PaymentDao pdao = new PaymentDao(session);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (request.getParameter("courseID") != null) {
            long userID = 0;
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals("userID")) {
                    if (cookie.getValue().length() > 0) {
                        userID = Long.parseLong(cookie.getValue());
                    }
                    break;
                }
            }
            if (userID != 0) {
                User user = udao.get(userID);
                Set<Course> courses = user.getStudentCourses();
                boolean subscribed = false;
                long courseID = Long.parseLong(request.getParameter("courseID"));
                Course userCourse = null;
                for (Course course : courses) {
                    if (course.getCourseID() == courseID) {
                        subscribed = true;
                        userCourse = course;
                        break;
                    }
                }
                if (subscribed == true) {
                    HttpSession sessie = request.getSession(true);
                    sessie.setAttribute("course", userCourse);
                    RequestDispatcher dispatcher = request.getRequestDispatcher("courseEnvironment.jsp");
                    dispatcher.forward(request, response);
                } else {
                    response.sendRedirect("/courses");
                }
            } else {
                response.sendRedirect("/index");
            }
        } else {
            response.sendRedirect("/courses");
        }
    }
    
    

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}
